#include "sessiondetailwidget.h"

#include <QVBoxLayout>
#include <QFontMetrics>
#include <QMessageBox>

#include "debug.h"
#include "choosefrienddialog.h"
#include "model/datacenter.h"

using namespace model;

/*
 * 表示在聊天窗口右上方的三个点打开的时候：一个头像和名字
 *
 */
AvatarItem::AvatarItem(const QIcon& avatar,const QString& name)
{
    //设置自身的属性
    this->setFixedSize(70,80);
    //创建布局管理器
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0,0,0,0);
    layout->setAlignment(Qt::AlignCenter);
    this->setLayout(layout);

    //创建头像
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(45,45);
    avatarBtn->setIconSize(QSize(45,45));
    avatarBtn->setIcon(avatar);
    avatarBtn->setStyleSheet("QPushButton { border: none; }");

    //创建名字
    nameLabel = new QLabel();
    nameLabel->setText(name);
    QFont font("微软雅黑", 12);
    nameLabel->setFont(font);
    nameLabel->setAlignment(Qt::AlignCenter);

    //如果名字太长需要做截断
    const int MAX_WIDTH = 65;
    QFontMetrics metrice(font);
    int totalWidth = metrice.horizontalAdvance(name);
    if(totalWidth >= MAX_WIDTH) {
        //需要截断
        QString tail = "...";
        //算出tail的宽度
        int tailWidth = metrice.horizontalAdvance(tail);
        int availableWidth = MAX_WIDTH - tailWidth;
        int availableSize = name.size() * ((double)availableWidth / totalWidth);
        QString newName = name.left(availableSize);
        nameLabel->setText(newName + tail);
    }

    //添加到布局管理器中
    layout->addWidget(avatarBtn);
    layout->addWidget(nameLabel);
}

/*
 * 单聊会话详情
 *
 */
SessionDetailWidget::SessionDetailWidget(QWidget* parent,const UserInfo& userInfo) :QDialog(parent),userInfo(userInfo)
{
    //设置基本属性
    this->setWindowTitle("会话详情");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setFixedSize(300,300);
    this->setStyleSheet("QWidget { background-color: rgb(255,255,255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    //创建布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setSpacing(10);
    layout->setContentsMargins(50,0,50,0);
    this->setLayout(layout);

    //添加 创建群聊 按钮
    AvatarItem* createGroupBtn = new AvatarItem(QIcon(":/resource/image/cross.png"),"添加");
    layout->addWidget(createGroupBtn,0,0);

//添加当前用户的信息
#if TEST_UI
    AvatarItem* currentItem = new AvatarItem(QIcon(":/resource/image/avatarDefault.png"),"不摇碧莲123");
    layout->addWidget(currentItem,0,1);
#endif
    AvatarItem* currentUser = new AvatarItem(userInfo.avatar,userInfo.nickname);
    layout->addWidget(currentUser,0,1);

    //添加删除好友按钮
    deleteFriendBtn = new QPushButton();
    deleteFriendBtn->setText("删除好友");
    deleteFriendBtn->setFixedHeight(50);
    QString style = "QPushButton { border: 1px solid rgb(90,90,90); border-radius: 5px; }";
    style += "QPushButton:pressed { background-color: rgb(235, 235, 235); }";
    deleteFriendBtn->setStyleSheet(style);
    layout->addWidget(deleteFriendBtn, 1, 0, 1, 3);

    //添加信号槽， 处理点击添加群聊按钮
    connect(createGroupBtn->getAvatar(),&QPushButton::clicked,this,[=](){
        ChooseFriendDialog* chooseFriendDialog = new ChooseFriendDialog(this,userInfo.userId);
        chooseFriendDialog->exec();
    });

    connect(deleteFriendBtn, &QPushButton::clicked, this, &SessionDetailWidget::clickDeleteFriendBtn);
}

void SessionDetailWidget::clickDeleteFriendBtn()
{
    auto result = QMessageBox::warning(this, "确认删除", "确认删除该好友?", QMessageBox::Ok | QMessageBox::Cancel);
    if (result != QMessageBox::Ok) {
        LOG() << "用户取消了好友删除";
        return;
    }

    DataCenter* dataCenter = DataCenter::getInstance();
    dataCenter->clickDeleteFriendAsync(this->userInfo.userId);

    this->close();
}


